<?php
require_once("includes/xmlparser.php");

class USPS {

    var $server = "";
    var $user = "";
    var $city = "";
    var $state = "";
    var $zipcode = "";
    var $address1 = "";
    var $address2 = "";
    var $country = "US";
    var $isOK = "N";
    
    function setServer($server) {
        $this->server = $server;
    }

    function setUserName($user) {
        $this->user = $user;
    }

    function setCity($city) {
        $this->city = $city;
    }
    
    function setState($val) {
        /* Must be 5 digit zip (No extension) */
        $this->state = $val;
    }

    function setZipCode($orig_zip) {
        $this->zipcode = $orig_zip;
    }   
    
    function setCountry($country) {
        $this->country = $country;
    }
    
    function setAddress1($address1) {
        $this->address1 = $address1;
    }
    
    function setAddress2($address2) {
        $this->address2 = $address2;
    }
       
    function verify_address(){
        $xmlParser = new xmlparser();
        $xml = '<AddressValidateRequest%20USERID="' . $this->user . '"><Address%20ID="0">';
        $xml .= '<Address1>' . $this->address1 . '</Address1>';
        $xml .= '<Address2>' . $this->address2 . '</Address2>';
        $xml .= '<City>' . $this->city . '</City>';
        $xml .= '<State>' . $this->state . '</State>';
        $xml .= '<Zip5>' . $this->zipcode . '</Zip5>';
        $xml .= '<Zip4></Zip4></Address></AddressValidateRequest>';
        $ch = curl_init();
        // set URL and other appropriate options              
        curl_setopt($ch, CURLOPT_URL,  $this->server."?API=Verify&XML=".$xml);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // grab URL and pass it to the browser
        $httpResponse = curl_exec($ch);
        
        // close curl resource, and free up system resources
        curl_close($ch);
        
        $array = $xmlParser->GetXMLTree($httpResponse);
        if(isset($array['ADDRESSVALIDATERESPONSE'])){
            if(isset($array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ERROR'])) { // If it is error
                $error = new error();
                $error->number = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ERROR'][0]['NUMBER'][0]['VALUE'];
                $error->source = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ERROR'][0]['SOURCE'][0]['VALUE'];
                $error->description = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
                $error->helpcontext = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ERROR'][0]['HELPCONTEXT'][0]['VALUE'];
                $error->helpfile = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ERROR'][0]['HELPFILE'][0]['VALUE'];
                $this->error = $error;
            } else{//everything is ok
                
                $this->address2 = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ADDRESS2'][0]['VALUE'];
                $this->city = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['CITY'][0]['VALUE'];
                $this->state = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['STATE'][0]['VALUE'];
                $this->zipcode = $array['ADDRESSVALIDATERESPONSE'][0]['ADDRESS'][0]['ZIP5'][0]['VALUE'];
                
                //print_r($array['ADDRESSVALIDATERESPONSE']);
                $this->isOK = "Y";
            }
        }
        return $this;
    }
}
class error
{
    var $number;
    var $source;
    var $description;
    var $helpcontext;
    var $helpfile;
}
?> 
